<template>
 <div class="login-container">
    <div class="login-form-wrap">
      <!-- logo区域 -->
      <div class="login-head">
        <div class="logo"></div>
      </div>
        <el-form class="login-form" :model="form" :rules="rules" ref="form">
            <el-form-item prop="mobile">
                <el-input placeholder="手机号" v-model.trim='form.mobile'></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input type="password" placeholder="密码" v-model.trim='form.code'></el-input>
            </el-form-item>
            <el-form-item  prop="isAgree">
                <el-checkbox label="我同意条款及协议" v-model="form.isAgree"></el-checkbox>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" class="login-btn" @click.prevent="hLogin('form')" :loading="isLoad">登陆</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>
<style lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
<script>
import axios from '../untils/request.js'
export default {
  data () {
    return {
      form: {
        isAgree: false,
        mobile: '',
        code: ''
      },
      isLoad: false,
      rules: {
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[345678]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\d{6,18}$/, message: '密码格式不正确', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => {
              console.log(rule, value, callback)
              if (value) {
                callback()
              } else {
                callback(new Error('请阅读并同意条款'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    Login () {
      this.isLoad = true
      axios({
        method: 'post',
        url: '/mp/v1_0/authorizations',
        data: this.form
      }).then(res => {
        this.$message({
          message: '恭喜你，登录成功',
          type: 'success'
        })
        console.log(res)
        this.isLoad = false
        localStorage.setItem('token', `Bearer ${res.data.data.token}`)
        this.$router.push('/')
      }).catch(err => {
        console.log(err)
        this.$message({
          message: '登录失败，请校验手机号和密码是否正确',
          type: 'warning'
        })
        this.isLoad = false
      })
    },
    hLogin (form) {
      if (!this.form.isAgree) {
        this.$message('请阅读并勾选我同意条款及协议')
        return
      }
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.Login()
        }
      })
    }
  }
}
</script>
